package no.ntnu.fp.mmi;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import no.ntnu.fp.su.Log;
import no.ntnu.fp.su.LogEntry;
import no.ntnu.fp.su.Status;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/

/**
 * Panel that contains the logs of selected well
 */
@SuppressWarnings("serial")
public class LogPanel extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel header;
	private JButton closeButton;
	private JRadioButton WarningsOnly;
	private JRadioButton showAll;
	private JButton viewComment;
	private JButton addComment;
	private JTable log;
	private TableRowSorter<TableModel> sorter;
	private ButtonGroup buttonGroup;
	private ArrayList<LogEntry> logs;
	private JScrollPane jScrollPane1;
	private DefaultTableModel modelle;
	private String name;
	private Log logModel;
	private LogPanel panel;
	
	/**
	 * Constructor of Logpanel 
	 */
	public LogPanel(ArrayList<LogEntry> logg, String name, Log logModel) {
		super();
		this.panel = this;
		this.logs = logg;
		this.name = name;
		this.logModel = logModel;
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			setSize(1000, 500);

			{
				Font font = new Font("Candera", Font.BOLD, 16);
				header = new JLabel();
				getContentPane().add(header);
				header.setText("Log for: " + this.name);
				header.setBounds(43, 12, 309, 25);
				header.setFont(font);
			}
			
			{
				closeButton = new JButton();
				getContentPane().add(closeButton);
				closeButton.setText("Close");
				closeButton.setBounds(43, 405, 100, 30);
				closeButton.addActionListener(new CloseButtonEvent());
			}
			{
				addComment = new JButton();
				getContentPane().add(addComment);
				addComment.setText("Add Comment");
				addComment.setBounds(43, 358, 107, 28);
				addComment.setEnabled(false);
				addComment.addActionListener(new addCommentEvent());
			}
			{
				viewComment = new JButton();
				getContentPane().add(viewComment);
				viewComment.setText("View Comment");
				viewComment.setBounds(156, 358, 112, 28);
				viewComment.setEnabled(false);
				viewComment.addActionListener(new viewCommentEvent());
			}
			{
				buttonGroup = new ButtonGroup();
				WarningsOnly = new JRadioButton();
				getContentPane().add(WarningsOnly);
				WarningsOnly.setText("Show Warnings Only");
				WarningsOnly.setBounds(121, 55, 157, 18);
				WarningsOnly.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						String text = "warning";
						sorter.setRowFilter(javax.swing.RowFilter.regexFilter(text));
					}
				});
				buttonGroup.add(WarningsOnly);
			}
			{
				showAll = new JRadioButton();
				getContentPane().add(showAll);
				showAll.setText("Show All");
				showAll.setBounds(43, 52, 134, 25);
				showAll.setSelected(true);
				showAll.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						String text = "";
						sorter.setRowFilter(javax.swing.RowFilter.regexFilter(text));
						
					}
				});
				buttonGroup.add(showAll);
				
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(43, 132, 861, 220);
				{
					
					String[] columnNames = {"Well ID","Time", "P", "T",
							"R","EV", "Comments" , "Status"};
					
					Object[][] data = {};
					
					modelle = new DefaultTableModel(data ,columnNames){
						@SuppressWarnings("unchecked")
						public Class getColumnClass(int column)
						{
							return getValueAt(0, column).getClass();
						}
						public boolean isCellEditable(int row, int col) {
							return false;
						}
						
					};	
					
					log = new JTable(modelle);		
					jScrollPane1.setViewportView(log);
					log.setBounds(43, 132, 470, 168);
					log.setPreferredScrollableViewportSize(new Dimension(500, 70));
					log.setFillsViewportHeight(true);
					log.getColumnModel().getColumn(0).setPreferredWidth(30);
					log.getColumnModel().getColumn(1).setPreferredWidth(150);
					log.getColumnModel().getColumn(2).setPreferredWidth(100);
					log.getColumnModel().getColumn(3).setPreferredWidth(100);
					log.getColumnModel().getColumn(4).setPreferredWidth(50);
					log.getColumnModel().getColumn(5).setPreferredWidth(50);
					log.getColumnModel().getColumn(6).setPreferredWidth(50);
					log.setRowHeight(30);
					log.getTableHeader().setReorderingAllowed(false);
					log.setAutoCreateRowSorter(true);
					sorter = new TableRowSorter<TableModel>(log.getModel());
					log.setRowSorter(sorter);
				
					log.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					log.getSelectionModel().addListSelectionListener(new ListSelectionListener() {	
						@Override
						public void valueChanged(ListSelectionEvent e) {
							addComment.setEnabled(true);
							viewComment.setEnabled(true);
							
						}
					});
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		setModel(logs);

	}
	
	/**
	 * Listener for close button, closes the logpanel
	 */
	public class CloseButtonEvent implements ActionListener {
		public void actionPerformed(ActionEvent e){
			dispose();
		}
		
	}
	/**
	 * recieves log arraylist and adds rows to panel for each entry in log
	 */
	public void setModel (ArrayList<LogEntry> logs){
		DecimalFormat df = new DecimalFormat("#.000000");
		for (int i = logs.size() -1; i > -1; i--) {
			modelle.addRow(new Object[] {logs.get(i).getWellID(), 
										logs.get(i).getTime().toString(), 
										logs.get(i).getP(),
										logs.get(i).getT(),
										df.format(logs.get(i).getR()), 
										df.format(logs.get(i).getDeltaR()), 
										logs.get(i).getCommentSize(),
										ImageRenderer(logs.get(i).getStatus())});
			
		}
		
	}
	
	/**
	 * draws LogPanel
	 */
	public void reDraw(){
		String abba = (log.getValueAt(log.getSelectedRow(), 6)).toString();
		int baab = Integer.parseInt(abba) + 1;
		log.setValueAt(baab, log.getSelectedRow(), 6);
		
		
	}
	
	public void enable(){
		log.setEnabled(true);
	}
	
	/** Listens to the add comment button and opens a window to write a comment
	 * 
	 * 
	 *
	 */
	public class addCommentEvent implements ActionListener {
		 
				public void actionPerformed(ActionEvent e) {
					int x = log.getSelectedRow();
					String tid1 = (String) log.getValueAt(x, 1);
					
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
					java.util.Date myToDate = null;
					Timestamp tid = null;
					try {
						myToDate = sdf.parse(tid1);
					} catch (ParseException e1) {
						e1.printStackTrace();
					}
					tid = new Timestamp(myToDate.getTime());
					
					
					LogEntry logent = logModel.getlogByTime(tid);
					AddCommentPanel inst = new AddCommentPanel(logent, panel);
					inst.setForeground(Color.BLACK);
					inst.setTitle("Add Comment");
					inst.setLocationRelativeTo(null);
					inst.setVisible(true);
					log.setEnabled(false);
				}
			}
	
	/**
	 * 
	 * views the comments of selected entry in the log
	 *
	 */
	public class viewCommentEvent implements ActionListener{
		 
				public void actionPerformed(ActionEvent arg0) {
					int x = log.getSelectedRow();
					String tid1 = (String) log.getValueAt(x, 1);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
					java.util.Date myToDate = null;
					Timestamp tid = null;
					try {
						myToDate = sdf.parse(tid1);
					} catch (ParseException e1) {
						e1.printStackTrace();
					}
					tid = new Timestamp(myToDate.getTime());
					LogEntry logent = logModel.getlogByTime(tid);
					if(logent.getCommentSize()==0)JOptionPane.showMessageDialog(null	, "This entry has no comments", "No Comments", JOptionPane.INFORMATION_MESSAGE);
					else {
					ViewCommentPanel inst = new ViewCommentPanel(logent);
					inst.setTitle("Comments");
					inst.setVisible(true);
					}
				}
			}
	
	
	
	public ImageIcon ImageRenderer(Status x){
		ImageIcon icon = null;
		switch(x){
		case RED:
			icon = new ImageIcon(getClass().getResource("warning2.gif"));
			break;
		case YELLOW:
			icon = new ImageIcon(getClass().getResource("warning.png"));
			break;
		case GREEN:
			icon = new ImageIcon(getClass().getResource("okey.png"));
			break;
		}
		
		
		return icon;
	}


	

}

